package com.fan.shop.businesslogic;
import org.apache.log4j.Logger;

import com.fan.shop.domain.TransactionReference;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.TransactionReferenceService;

/**
 * 
 */

/**
 * @author fjin
 *
 */
public class TransactionReferenceCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(TransactionReferenceCreateProcess.class);
	
	private TransactionReferenceService transactionReferenceService;
	private String name;
	private String prefix;
	private String description;
	private int start;
	private int last;
	private int max;
	private String groupCode;
	
	
	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLast() {
		return last;
	}

	public void setLast(int last) {
		this.last = last;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public String getGroupCode() {
		return groupCode;
	}

	public void setGroupCode(String groupCode) {
		this.groupCode = groupCode;
	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TransactionReferenceCreateProcess.logger = logger;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * 
	 */
	public TransactionReferenceCreateProcess() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Integer processImpl() {
		
		if(transactionReferenceService.hasTransactionReference(name))
		{
			logger.debug("Transaction Reference: " + name + " already exists!");
			return ShopGlobalVariables.FAILED;
		}
		else
		{
			TransactionReference transactionReference = new TransactionReference();
			transactionReference.setName(name);
			transactionReference.setGroupCode(groupCode);
			transactionReference.setPrefix(prefix);
			transactionReference.setDescription(description);
			if(start <= 0)
			{
				start = 1;
			}
			if(last < start)
			{
				last = start;
			}
			if(max < last)
			{
				max = last;
			}
			
			transactionReference.setStart(start);
			transactionReference.setLast(last);
			transactionReference.setMax(max);
			
			
			transactionReferenceService.addTransactionReference(transactionReference);
			return ShopGlobalVariables.SUCCESS;
		}
		
	}

}
